package com.rp.activiti7.security;

import com.rp.activiti7.mapper.UserInfoBeanMapper;
import com.rp.activiti7.pojo.UserInfoBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

@Component
public class MyUserDetailsService implements UserDetailsService {

    @Autowired
    private UserInfoBeanMapper userInfoBeanMapper;

    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//        String password ="111";
//
//        return new User(username,password, AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_ACTIVITI_USER"));
        UserInfoBean userInfoBean = userInfoBeanMapper.selectByUsername(username);
        if (userInfoBean == null) {
            throw new UsernameNotFoundException("查询不到该用户");
        }
        return userInfoBean;


    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

}
